package util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class StubEmitter {
	
	private static final String strings[] = {
		"ADD",
		"ADDI",
		"ADDIU",
		"ADDU",
		"AND",
		"ANDI",
		"BC",
		"BCF",
		"BCFL",
		"BCT",
		"BCTL",
		"BEQ",
		"BEQL",
		"BGEZ",
		"BGEZAL",
		"BGEZALL",
		"BGEZL",
		"BGTZ",
		"BGTZL",
		"BLEZ",
		"BLEZL",
		"BLTZ",
		"BLTZAL",
		"BLTZALL",
		"BLTZL",
		"BNE",
		"BNEL",
		"BREAK",
		"CACHE",
		"CF",
		"CO",
		"COP0",
		"COP1",
		"COP2",
		"CT",
		"DADD",
		"DADDI",
		"DADDIU",
		"DADDU",
		"DDIV",
		"DDIVU",
		"DIV",
		"DIVU",
		"DMF",
		"DMT",
		"DMULT",
		"DMULTU",
		"DSLL",
		"DSLL32",
		"DSLLV",
		"DSRA",
		"DSRA32",
		"DSRAV",
		"DSRL",
		"DSRL32",
		"DSRLV",
		"DSUB",
		"DSUBU",
		"ERET",
		"J",
		"JAL",
		"JALR",
		"JR",
		"LB",
		"LBU",
		"LD",
		"LDC1",
		"LDC2",
		"LDL",
		"LDR",
		"LH",
		"LHU",
		"LL",
		"LLD",
		"LUI",
		"LW",
		"LWC1",
		"LWC2",
		"LWL",
		"LWR",
		"LWU",
		"MF",
		"MFHI",
		"MFLO",
		"MT",
		"MTHI",
		"MTLO",
		"MULT",
		"MULTU",
		"NOR",
		"OR",
		"ORI",
		"REGIMM",
		"SB",
		"SC",
		"SCD",
		"SD",
		"SDC1",
		"SDC2",
		"SDL",
		"SDR",
		"SH",
		"SLL",
		"SLLV",
		"SLT",
		"SLTI",
		"SLTIU",
		"SLTU",
		"SPECIAL",
		"SRA",
		"SRAV",
		"SRL",
		"SRLV",
		"SUB",
		"SUBU",
		"SW",
		"SWC1",
		"SWC2",
		"SWL",
		"SWR",
		"SYNC",
		"SYSCALL",
		"TEQ",
		"TEQI",
		"TGE",
		"TGEI",
		"TGEIU",
		"TGEU",
		"TLBP",
		"TLBR",
		"TLBWI",
		"TLBWR",
		"TLT",
		"TLTI",
		"TLTIU",
		"TLTU",
		"TNE",
		"TNEI",
		"UNHANDLED",
		"XOR",
		"XORI"
	};
	
	public static void main(final String[] args) throws IOException {
		
		final StubEmitter newStubEmitter = new StubEmitter();

		for(int i=0;i<strings.length;i++) {
			final String insName = strings[i];
			
			final File testFile = new File(insName + ".java");
			final Writer output = new BufferedWriter(new FileWriter(testFile));

			output.write( newStubEmitter.getCode(insName) );
			output.close();
		}
	}
	
	private final String getCode(final String insName) {
	
		String code = "";
		
		code += "package mips.instructions;\n";
		code += "\n";
		code += "public class " + insName + " extends Instruction {\n";
		code += "\n";
		code += "\tprivate static final String insName = \"" + insName + "\";\n";
		code += "\n";
		code += "\tprivate static " + insName + " instance = null;\n";
		code += "\n";
		code += "\n";
		code += "\tprivate " + insName + "() {\n";
		code += "\n";
		code += "\t}\n";
		code += "\n";
		code += "\n";
		code += "\tpublic static " + insName + " getInstance() {\n";
		code += "\n";
		code += "\t\tif(instance == null) {\n";
		code += "\n";		
		code += "\t\t\tinstance = new " + insName + "();\n";
		code += "\t\t}\n";
		code += "\n";
		code += "\t\treturn instance;\n";
		code += "\t}\n";
		code += "\n";
		code += "\n";
		code += "\tpublic final String getName() {\n";
		code += "\n";
		code += "\t\treturn insName;\n";
		code += "\t}\n";
		code += "}";
	
		return code;
	}
}
